import QtQuick 2.6
import QtQuick.Layouts 1.2

import "../../Base/UiBase"
import "../../Base/Config"

Rectangle {
    id: root
    property alias headSource: head.headSource
    property alias username: head.username
    property alias subject: head.subject
    property alias time: head.time
    property alias location: head.location
    property int imageCount: 0
    property alias imageSource: image.source
    property alias commentText: comment.text

    property alias upActivated: foot.upActivated
    property alias submitingComment: foot.upActivated
    property alias upNumber: foot.upNumber

    signal commentPageClicked

    height: contentColumn.height
    color: Color.foreground

    Column {
        id: contentColumn

        width: parent.width
        spacing: Size.elementInterval

        PostHead {
            id: head
            width: parent.width
        }

        Column {
            id: mainContent

            width: root.width
            spacing: Size.elementInterval


            SvgzImage {
                id: image

                property double minimunHeight: 0
                property double maximunHeight: Size.advPost.mainImageMaximunHeight

                onMinimunHeightChanged: resize()
                onMaximunHeightChanged: resize()
                onSourceSizeChanged: resize()
                onHeightChanged: {
                    if (height > maximunHeight)
                        height = maximunHeight
                    if (height < minimunHeight)
                        height = minimunHeight
                }
                function resize() {
                    height = sourceSize.height
                    if(height === 0)
                        height = minimunHeight // when (sourceSize.height === 0), heightChanged is not emitted
                }

                anchors.left: parent.left
                anchors.right: parent.right
                fillMode: SvgzImage.PreserveAspectCrop

                Text {
                    anchors {
                        right: parent.right
                        bottom: parent.bottom
                    }
                    visible: imageCount > 1
                    text: "+" + String(imageCount) + qsTr("张图  ", "??张图  ")
                    font.pixelSize: Size.normalFont
                    Rectangle {
                        z: -1
                        anchors.fill: parent
                        color: Qt.rgba(1, 1, 1, 0.35)
                        radius: 10 * Size.ratio
                    }
                }
            }

            Text {
                id: comment
                property double minimumHeight: 0
                maximumLineCount: 12

                function resize() {
                    if (text.length > 0)
                        height = contentHeight
                    else
                        // if(height === contentHeight === 0), heightChanged will not generate.
                        height = minimumHeight
                }

                onHeightChanged: {
                    if (height < minimumHeight)
                        height = minimumHeight
                }
                onContentHeightChanged: resize()
                onMinimumHeightChanged: resize()

                anchors {
                    left: parent.left
                    right: parent.right
                    leftMargin: 10 * Size.ratio
                    rightMargin: 10 * Size.ratio
                }
                font.pixelSize: Size.normalFont
                wrapMode: Text.Wrap
                elide: Text.ElideRight
            }
            Item {
                width: parent.width
                height: Size.elementInterval
            }
        }

        PostFoot {
            id: foot

            onCommentPageClicked: root.commentPageClicked()
        }
    }
}
